/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DangNhapDialog.java
 *
 * Created on Jun 30, 2010, 7:09:17 PM
 */

package GUI;

import BUS.NguoiDungBUS;
import POJO.NguoiDungPOJO;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * @author
 *  ITEC.HCMUS.EDU.VN
 *  Ngô Huy Đạt
 *  0869005
 *  nhdat.itec@gmail.com
 */
public class DangNhapDialog extends javax.swing.JDialog {

    NguoiDungPOJO NguoiDung = new NguoiDungPOJO();
    private boolean KQDangNhap;

    public boolean getKQDangNhap() {
        return KQDangNhap;
    }

    public NguoiDungPOJO getNguoiDung() {
        return NguoiDung;
    }

    /** Creates new form DangNhapDialog */
    public DangNhapDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.KQDangNhap=false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTF_TenDangNhap = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPF_MatKhau = new javax.swing.JPasswordField();
        jB_DangNhap = new javax.swing.JButton();
        jBThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản lý học sinh - Đăng nhập");
        setIconImage(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin đăng nhập"));

        jLabel1.setText("Tài khoản:");

        jTF_TenDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTF_TenDangNhapActionPerformed(evt);
            }
        });

        jLabel2.setText("Mật khẩu:");

        jPF_MatKhau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPF_MatKhauActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPF_MatKhau)
                    .addComponent(jTF_TenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTF_TenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPF_MatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jB_DangNhap.setText("Đăng nhập");
        jB_DangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_DangNhapActionPerformed(evt);
            }
        });

        jBThoat.setText("Thoát");
        jBThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jB_DangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jB_DangNhap)
                    .addComponent(jBThoat))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jB_DangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_DangNhapActionPerformed
        // TODO add your handling code here:
        ArrayList<NguoiDungPOJO> list = NguoiDungBUS.selectAll();
        int size = list.size();
        for(int i=0;i<size;i++){
            if(this.jTF_TenDangNhap.getText().equals(list.get(i).getTaiKhoan())
              && this.jPF_MatKhau.getText().equals(list.get(i).getMatMa())){
                this.NguoiDung = list.get(i);
                this.KQDangNhap = true;
                break;
            }
        }
        if(this.KQDangNhap==false){
            JOptionPane.showMessageDialog(this, "Sai tên đang nhập hoặc mật khẩu !", "Thông báo", JOptionPane.ERROR_MESSAGE);
            this.jTF_TenDangNhap.requestFocus();
            this.jTF_TenDangNhap.selectAll();
            this.jPF_MatKhau.setText(null);
        }else{
            this.setVisible(false);
        }
    }//GEN-LAST:event_jB_DangNhapActionPerformed

    private void jBThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jBThoatActionPerformed

    private void jPF_MatKhauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPF_MatKhauActionPerformed
        // TODO add your handling code here:
        this.jB_DangNhapActionPerformed(evt);
    }//GEN-LAST:event_jPF_MatKhauActionPerformed

    private void jTF_TenDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTF_TenDangNhapActionPerformed
        // TODO add your handling code here:
        this.jB_DangNhapActionPerformed(evt);
    }//GEN-LAST:event_jTF_TenDangNhapActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DangNhapDialog dialog = new DangNhapDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBThoat;
    private javax.swing.JButton jB_DangNhap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPF_MatKhau;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTF_TenDangNhap;
    // End of variables declaration//GEN-END:variables

}
